package org.exercise9.mediator;

import java.util.ArrayDeque;
import java.util.Objects;
import java.util.Queue;

/**
 * @author Stranger
 * @version 1.0
 * @description: TODO
 * @date 2022/12/20 16:13
 */
public abstract class AbstractMediator implements Mediator {

    protected Queue<String> messageQueue;

    public AbstractMediator() {
        this.messageQueue = new ArrayDeque<>();
    }

    @Override
    public String message() {
        return messageQueue.peek();
    }

    @Override
    public String messageAndRemove() {
        return messageQueue.poll();
    }

    @Override
    public void exchange(Mediator mediator) {
        if (Objects.isNull(mediator)) return;
        messageQueue.forEach(mediator::sendMessage);
    }

    @Override
    public void sendMessage(String message) {
        if (Objects.isNull(message)) return;
        messageQueue.add(message);
    }
}
